package com.fr.report.web.ui;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.ComparatorUtils;
import com.fr.base.core.json.JSONArray;
import com.fr.base.core.json.JSONException;
import com.fr.base.core.json.JSONObject;
import com.fr.base.core.util.TemplateUtils;
import com.fr.base.xml.XMLPrintWriter;
import com.fr.base.xml.XMLableReader;
import com.fr.data.Dictionary;
import com.fr.data.Dictionary.MV;
import com.fr.data.core.DataXMLUtils;
import com.fr.report.io.xml.ReportXMLUtils;
import com.fr.report.script.Calculator;
import com.fr.util.Utils;
import com.fr.web.Repository;
import com.fr.web.core.SessionIDInfor;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;

public class TableTree extends FieldEditor
{
  public static final String XML_TAG = "TT";
  private String dataUrl;
  private Dictionary dict;

  public String getXType()
  {
    return "tabletree";
  }

  public String getDataUrl()
  {
    return this.dataUrl;
  }

  public void setDataUrl(String paramString)
  {
    this.dataUrl = paramString;
  }

  public Dictionary getDictionary()
  {
    return this.dict;
  }

  public void setDictionary(Dictionary paramDictionary)
  {
    this.dict = paramDictionary;
  }

  public JSONArray createJSONData(SessionIDInfor paramSessionIDInfor, Calculator paramCalculator, HttpServletRequest paramHttpServletRequest)
    throws Exception
  {
    JSONArray localJSONArray = super.createJSONData(paramSessionIDInfor, paramCalculator, paramHttpServletRequest);
    if (this.dict == null)
      return localJSONArray;
    this.dict.reset();
    Iterator localIterator = this.dict.entrys(paramCalculator);
    while (localIterator.hasNext())
    {
      Dictionary.MV localMV = (Dictionary.MV)localIterator.next();
      localJSONArray.put(new JSONObject().put("state", Utils.objectToString(localMV.getModel())).put("name", Utils.objectToString(localMV.getView())));
    }
    return localJSONArray;
  }

  public JSONObject createJSONConfig(Object paramObject, Repository paramRepository)
    throws JSONException
  {
    JSONObject localJSONObject = super.createJSONConfig(paramObject, paramRepository);
    Map localMap = paramRepository.checkoutTplContext();
    try
    {
      localJSONObject.put("url", TemplateUtils.render(this.dataUrl, localMap));
    }
    catch (Exception localException)
    {
      FRContext.getLogger().log(Level.WARNING, localException.getMessage(), localException);
    }
    return localJSONObject;
  }

  public void readXML(XMLableReader paramXMLableReader)
  {
    super.readXML(paramXMLableReader);
    if (paramXMLableReader.isChildNode())
    {
      String str = paramXMLableReader.getTagName();
      if (str.equals("TT"))
        this.dataUrl = paramXMLableReader.getAttr("url");
      else if (str.equals("Dictionary"))
        this.dict = DataXMLUtils.readXMLDictionary(paramXMLableReader);
    }
  }

  public void writeXML(XMLPrintWriter paramXMLPrintWriter)
  {
    super.writeXML(paramXMLPrintWriter);
    if (!(StringUtils.isEmpty(this.dataUrl)))
      paramXMLPrintWriter.startTAG("TT").attr("url", this.dataUrl).end();
    if (this.dict != null)
      ReportXMLUtils.writeXMLable(paramXMLPrintWriter, this.dict, "Dictionary");
  }

  public boolean equals(Object paramObject)
  {
    return ((paramObject instanceof TableTree) && (super.equals(paramObject)) && (ComparatorUtils.equals(this.dataUrl, ((TableTree)paramObject).dataUrl)));
  }
}